package com.gouzheng.service.impl;

import com.gouzheng.dao.PhoneDao;
import com.gouzheng.entity.Phone;
import com.gouzheng.entity.User;
import com.gouzheng.service.PhoneService;
import com.gouzheng.util.SessionCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
@Service
public class PhoneServiceImpl implements PhoneService {

    @Autowired
    private PhoneDao phoneDao;

    @Override
    public Boolean selectPhone(String phone, HttpSession session) {
        User user = phoneDao.selectPhone(phone);
        if(user==null){
            System.out.println("手机号没注册");
            return false;
        }
        int random=(int) (Math.random() * 1000000);
        System.out.println(random);
        String massage="{\"code\":\""+random+"\"}";
        Phone.setMessage(phone,massage);
        session.setAttribute("ac",user);
        session.setAttribute("massage",massage);
        SessionCache.getSessionCache().saveSession(session);
        return true;
    }

    @Override
    public Boolean phoneLogin(String massage, HttpSession session) {
        String scc=null;
        if(massage!=null){
            scc="{\"code\":\""+massage+"\"}";
        }
        String massage1=(String) session.getAttribute("massage");
        if(massage1.equals(scc)){
            Object ac=session.getAttribute("ac");
            session.setAttribute("cc",ac);
            return true;
        }

        return false;
    }
}
